# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=shaderc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2023.6
pkgrel=2
pkgdesc="Collection of tools, libraries and tests for shader compilation (mingw-w64)"
url="https://github.com/google/shaderc"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glslang"
         "${MINGW_PACKAGE_PREFIX}-spirv-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers"
             "${MINGW_PACKAGE_PREFIX}-cc"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || \
               echo "${MINGW_PACKAGE_PREFIX}-asciidoctor"))
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/shaderc/archive/v${pkgver}.tar.gz"
        "0001-fix-glslang-hlsl-linking-order.patch")
sha512sums=('6580384ac27c4f2225529873ba46ca61240bdf63f4f5b89875e9bd20a774fed49328094379b7f469c3b97b6a3744cb649998c0d6d5a391023317a64a91754bb1'
            '598c8e32196ac015e5d7e486717163425d9b233851f23540fe4d6a79b86ba07dd6804e9d2e4776a3e0f5c33f5057db0a31b03dccc9a49c66971eb34d32e01a7e')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://www.mail-archive.com/devel@lists.fedoraproject.org/msg156020.html
  patch -Nbp1 -i "${srcdir}/0001-fix-glslang-hlsl-linking-order.patch"

  # de-vendor libs and disable git versioning
  sed '/examples/d;/third_party/d' -i CMakeLists.txt
  sed '/build-version/d' -i glslc/CMakeLists.txt
  cat <<- EOF > glslc/src/build-version.inc
"${pkgver}\\n"
"$(pacman -Q "${MINGW_PACKAGE_PREFIX}-spirv-tools"|cut -d \  -f 2|sed 's/-.*//')\\n"
"$(pacman -Q "${MINGW_PACKAGE_PREFIX}-glslang"|cut -d \  -f 2|sed 's/-.*//')\\n"
EOF
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  local -a _extra_config
  if check_option "debug" "y"; then
    _extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    _extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  local -a _glslang_inc
  _glslang_inc=$(cygpath -m "${MINGW_PREFIX}/include/glslang")

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    CXXFLAGS+=" -Wno-c++11-narrowing"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -Dglslang_SOURCE_DIR=${_glslang_inc} \
    -DSHADERC_SKIP_TESTS=ON \
    -DSHADERC_ENABLE_WERROR_COMPILE=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  # Generate manpage
  # Suppress asciidoctor: ERROR: line 1: non-conforming manpage title
  cd "${srcdir}/${_realname}-${pkgver}/glslc"
  ${MINGW_PREFIX}/bin/asciidoctor -b manpage README.asciidoc -o glslc.1 || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/glslc/glslc.1" \
  -t "${pkgdir}${MINGW_PREFIX}/share/man/man1" || true
}
